package com.skwen.delivery.superdelivery.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.skwen.delivery.superdelivery.login.bean.User;

/**
 * Created by yundantian on 2017/10/12.
 *
 */

public class SpUtil {


    private Context mContext;
    private static SpUtil mSpUtil;
    private SharedPreferences sp;

    private SpUtil(Context context) {
        sp = context.getSharedPreferences(MyConstant.user, Context.MODE_PRIVATE);
    }


    public static SpUtil getInstance(Context context) {
        if (mSpUtil == null) {
            mSpUtil = new SpUtil(context);
        }
        return mSpUtil;
    }


    public void saveUser(User user) {
        if (user != null) {
            if (!TextUtils.isEmpty(user.getUserAccount()))
                sp.edit().putString(MyConstant.userName, user.getUserAccount()).apply();
            if (!TextUtils.isEmpty(user.getUserPsd()))
                sp.edit().putString(MyConstant.userPsd, user.getUserPsd()).apply();
        }
    }

    public String getString(String key) {
        return sp.getString(key, "");
    }


    public void saveString(String key, String value) {
        sp.edit().putString(key, value).apply();
    }


    public void saveBoolean(String key, boolean b) {
        sp.edit().putBoolean(key, b).apply();
    }

    public boolean getBoolean(String key) {
        return sp.getBoolean(key, false);
    }


}
